package com.lucher.mes.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lucher.mes.common.HttpRequest;
import com.lucher.mes.common.Tools;
import com.lucher.mes.common.Uuid;
import com.lucher.mes.dao.PrisonerMapper;
import com.lucher.mes.dao.RollcallMapper;
import com.lucher.mes.dao.RollcallinfoMapper;
import com.lucher.mes.po.Prisoner;
import com.lucher.mes.po.Rollcall;
import com.lucher.mes.po.Rollcallinfo;
import com.lucher.mes.service.ISystemparaService;

@Controller
@RequestMapping("/rollcall")  
public class RollcallController extends BaseController{
	@Autowired
	private RollcallMapper rollcallDao;
	@Autowired
	private RollcallinfoMapper rollcallinfoDao;
	@Autowired
	private PrisonerMapper prisonerDao;
	@Autowired
	private ISystemparaService systemparaService;
	
	/**
	 * 查询信息
	 */
	@RequestMapping(value = "/findByList")
	public String findByList(Rollcall form) {
		return "rollcall/findRollcallList";
	}
	
	/**
	 * 查询信息Json
	 */
	@ResponseBody
	@RequestMapping(value = "/findListJson")
	public Map<String, Object> findListJson(Rollcall form) {
		//封装JSon的Map
		Map<String, Object> result = new HashMap<String, Object>(); 
		result.put("code", "0");
		List<HashMap<String, Object>> objectlist = new ArrayList<HashMap<String, Object>>();
		
		Integer total = rollcallDao.findByCount(form);
		List<Rollcall> list = rollcallDao.findByListForPage(form);
		
		for (Rollcall rollcall : list) {
			HashMap<String, Object> objectMap = new HashMap<String, Object>();
			
			objectMap.put("id", rollcall.getId());
			objectMap.put("uuid", rollcall.getUuid());
			objectMap.put("addtime", StringUtils.isEmpty(rollcall.getAddtime())?"":Tools.getStr(rollcall.getAddtime()).substring(0, 19));
			objectMap.put("duration", rollcall.getUuid());
			objectMap.put("remark", rollcall.getRemark());
			
			objectlist.add(objectMap);
		}
		
		result.put("count", total);
		result.put("data", objectlist);
		return result;
	}

	/**
	 * 添加信息初始化
	 * 
	 * @return
	 */
	@RequestMapping(value = "/addInit")
	public String addInit(Rollcall form) {
		return "rollcall/addRollcall";
	}

	/**
	 * 新增
	 */
	@ResponseBody //此标志就是返回jesion数据给页面的标志
	@RequestMapping(value = "/save")
	public Map<String,Object>  save(Rollcall form) {
		
		//封装返回给页面的json对象
		HashMap<String,Object> responseJson = new HashMap<String,Object>();
		responseJson.put("code", "0");
		
		String uuid = Uuid.generateShortUuid();
		
		if ("".equals(uuid)) {
			responseJson.put("flag", "0"); //操作失败
			responseJson.put("msg", "操作失败，UUID不能为空");
			return responseJson;
		} else {
			Rollcall oldRollcall = rollcallDao.findByUuid(uuid);
			if (oldRollcall != null) {
				responseJson.put("flag", "0");//操作失败
				responseJson.put("msg", "操作失败，此UUID已经存在");
				return responseJson;
			}
		}
		
		//保存材料信息
		String cuurenttime = Tools.getCurrentTime();
		form.setUuid(uuid);
		form.setAddtime(cuurenttime);
		Integer rollcallId = rollcallDao.insertSelective(form);
		
		//批量插入所有人员的签到准备信息
		ArrayList<Rollcallinfo> rollcallinfolist = new ArrayList<Rollcallinfo>();
		//查询所有的人员
		Prisoner record = new Prisoner();
		//record.setStatus("入狱");
		List<Prisoner> prisonerlist = prisonerDao.findPrisonerList(record);
		for (Prisoner prisoner: prisonerlist) {
			Rollcallinfo rollcallinfo = new Rollcallinfo();
			rollcallinfo.setUuid(uuid);
			rollcallinfo.setPrisonercode(prisoner.getPrisonercode());
			rollcallinfo.setPrisonername(prisoner.getPrisonername());
			rollcallinfo.setStatus("未签到");
			rollcallinfolist.add(rollcallinfo);
		}
		if(rollcallinfolist != null && rollcallinfolist.size() > 0){
			rollcallinfoDao.saveBatch(rollcallinfolist);
		}
		responseJson.put("flag", "1");//操作成功
		responseJson.put("msg", "操作成功");
		responseJson.put("uuid",uuid);
		
		//发送点名指令
		String send_rollcall_url = systemparaService.findSystemParaByCodeStr("send_rollcall_url");
		String sr= HttpRequest.sendPost(send_rollcall_url, "uuid=" + uuid);
		
		return responseJson;
	}

	/**
	 * 更新初始化
	 */
	@RequestMapping(value = "/updateInit")
	public String updateInit(Rollcall form) {
		Rollcall rollcall = rollcallDao.selectByPrimaryKey(form.getId());
		form.setRollcall(rollcall);

		return "rollcall/updateRollcall";
	}

	/**
	 * 更新
	 */
	@ResponseBody //此标志就是返回jesion数据给页面的标志
	@RequestMapping(value = "/update")
	public Map<String,Object> update(Rollcall form) {
		//封装返回给页面的json对象
		HashMap<String,Object> responseJson = new HashMap<String,Object>();
		responseJson.put("code", "0");

		// 修改信息
		int flag = rollcallDao.updateByPrimaryKeySelective(form);
		
		responseJson.put("flag", "1");//
		responseJson.put("msg", "操作成功");
		return responseJson;
	}
    
	/**
	 * 删除
	 * @return
	 */
	@ResponseBody //此标志就是返回jesion数据给页面的标志
	@RequestMapping(value="/delete")
	public Map<String,Object> delete(Rollcall form) {
		//封装返回给页面的json对象
		HashMap<String,Object> responseJson = new HashMap<String,Object>();
		responseJson.put("code", "0");
		//删除材料
		rollcallDao.deleteByPrimaryKey(form.getId());
		
		responseJson.put("flag", "1");//删除成功
		responseJson.put("msg", "操作成功");
		
		return responseJson;
	}
	
	/**
	 * 批量删除
	 * @return
	 */
	@ResponseBody //此标志就是返回jesion数据给页面的标志
	@RequestMapping(value="/deleteBatch")
	public Map<String,Object> deleteBatch(@RequestBody List<Rollcall> formlist) {
		//封装返回给页面的json对象
		HashMap<String,Object> responseJson = new HashMap<String,Object>();
		responseJson.put("code", "0");
		for (Rollcall rollcall : formlist) {
			//删除材料
			rollcallDao.deleteByPrimaryKey(rollcall.getId());
		}

		responseJson.put("flag", "1");//删除成功
		responseJson.put("msg", "操作成功");
		
		return responseJson;
	}
	
}
